package com.bkq.cb;

import com.bkq.cb.BLO.UserBUO;
import com.bkq.cb.DTO.UserDTO;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.database.Cursor;
public class Register extends Activity{
	private Button backBnt,createBnt,cancelBnt;
	private SqliteServices service = null;
	private EditText username, password,name;
	private Cursor mCursor;
	UserBUO userbto=new UserBUO(this);
	  public void onCreate(Bundle savedInstanceState) {
			 super.onCreate(savedInstanceState);
		     setContentView(R.layout.register);
		     backBnt = (Button)findViewById(R.id.backIma_register);
		     createBnt = (Button)findViewById(R.id.createBnt_register);
		     cancelBnt = (Button)findViewById(R.id.cancelBnt_register);
		     name = (EditText)findViewById(R.id.fullNameTxt_register);
		     username = (EditText)findViewById(R.id.usernameTxt_register);
		     password = (EditText)findViewById(R.id.passwordTxt_register);
		     backBnt.setOnClickListener(new View.OnClickListener() {
				
				@Override
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					finish();
				}
			});
		    cancelBnt.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					finish();
				}
			}) ;

	        service = new SqliteServices(this);
	        service.openDatabase();
	        createBnt.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(name.getText().toString().equals(""))
					{
						Toast.makeText(getBaseContext(), "Full Name is null", Toast.LENGTH_SHORT).show();
					}
					else if(username.getText().toString().equals(""))
					{
						Toast.makeText(getBaseContext(), "Username is null", Toast.LENGTH_SHORT).show();

					}
					else if(password.getText().toString().equals(""))
					{
						Toast.makeText(getBaseContext(), "Password is null", Toast.LENGTH_SHORT).show();

					}
					else
					{
					UserDTO user=new UserDTO();	
					boolean bool = true;
					user.UserName=username.getText().toString();
					user.PassWord=password.getText().toString();
					user.Name=name.getText().toString();
					mCursor = service.queryData("select * from tblUser");
			
					if(mCursor.moveToFirst())
			        {
						if(mCursor.getString(2).equals(username.getText().toString()))
		        		{
		        			bool = false;
		        		}
			        	while(mCursor.moveToNext())
			        	{
			        		if(mCursor.getString(2).equals(username.getText().toString()))
			        		{
			        			bool = false;
								break;
			        		}
			        	}
			        }
			        	
					try
					{
						 if(bool == true)
						{
					    if(userbto.inser(user))
						{
						    name.setText("");
						    username.setText("");
						    password.setText("");
							Toast.makeText(getBaseContext(), "Registration success", Toast.LENGTH_SHORT).show();
						}
						else
						{
							Toast.makeText(getBaseContext(), "Registration failure", Toast.LENGTH_SHORT).show();

						//	textmess.setText("Khong Thanh cong");
						}
						}
						else 
						{
							 name.setText("");
							 username.setText("");
							 password.setText("");
							Toast.makeText(getBaseContext(), "Username already exists", Toast.LENGTH_SHORT).show();
						}
						 mCursor.close();
					    }
						
						
					       
					catch(Exception ex)
					{
						//textmess.setText("Khong Thanh cong");
						Toast.makeText(getBaseContext(), ex.getMessage().toString(), Toast.LENGTH_SHORT).show();

					}
					}
				}
			});
			
	  }
}
	  
